#include "main.h"
#include "board.h"
//#include "filelib.h"

#define ELF_BCFG_CONFIG_EVENT 994


BOOK   *MSBook;
MyBOOK *MMSBook;
char     mem;
PROCESS  pid;
int count;
int sfol=0;
int fliemode=0;

//char LT[]={"Show info"};
wchar_t imageID;


int TerminateElf ( void *, BOOK *book )
{
   FreeBook ( book );
   return 1;
}

int ShowAuthorInfo ( void *mess, BOOK *book )
{
  wchar_t s[128];
  snwprintf(s,128,L"Memory View v0.97 \n\nBy Zylka\nThanks ndt\n%s %s",__DATE__,__TIME__);
   MessageBox ( 0x6fFFFFFF, Str2ID(s,0,SID_ANY_LEN), 0, 1 , 5000, 0 );
   return 1;
}

/*
int onAccept_DB ( void *data, BOOK *book )
{
   return 0;
}

int CreateDB ( void *data, BOOK *book )
{
   return 0;
}

static int onCancelDB ( void *data, BOOK *book )
{
   FreeBook ( book );
   return 1;
}
*/

static int ReconfigElf (void *mess ,BOOK *book)
{
  RECONFIG_EVENT_DATA *rcfg = (RECONFIG_EVENT_DATA *) mess;

  if ( !wstrcmpi ( rcfg->path, successed_config_path ) && !wstrcmpi ( rcfg->name, successed_config_name ) )
  {
    InitConfig();
    return 1;
  }

  return 0;
}

int onBcfgConfig ( void *mess, BOOK *b )
{
   FSTAT fbuf;
   wchar_t path[256];

   if ( !fstat ( GetDir ( DIR_ELFS | MEM_INTERNAL ), L"BcfgEdit.elf", &fbuf ) )
      wstrcpy ( path, GetDir ( DIR_ELFS | MEM_INTERNAL ) );
   else if ( !fstat ( GetDir ( DIR_ELFS | MEM_EXTERNAL ), L"BcfgEdit.elf", &fbuf ) )
      wstrcpy ( path, GetDir ( DIR_ELFS | MEM_EXTERNAL ) );
   else
   {
      MessageBox ( 0x6FFFFFFF, STR ( "BcfgEdit.elf not found!" ), 0, 1 , 5000, 0 );
      return 0;
   }

   wstrcat ( path, L"/BcfgEdit.elf" );
   elfload ( path, (void*) successed_config_path, (void*) successed_config_name, 0 );
   return 1;
}

/*
const PAGE_MSG bk_msglst_main[] @ "DYN_PAGE"  =
{
  PAGE_ENTER_EVENT_TAG,       CreateDB,
  ACCEPT_EVENT_TAG,           onAccept_DB,
  PREVIOUS_EVENT_TAG,         onCancelDB,
  CANCEL_EVENT_TAG,           onCancelDB,
  NIL_EVENT_TAG,              NULL
};*/
//const PAGE_DESC bk_main = {"MemoryView_main",0,bk_msglst_main};

const PAGE_MSG MS_PageEvents[]@ "DYN_PAGE" = {
   ELF_TERMINATE_EVENT,       TerminateElf,
   ELF_SHOW_INFO_EVENT,       ShowAuthorInfo,
   ELF_RECONFIG_EVENT,        ReconfigElf,
   ELF_BCFG_CONFIG_EVENT,     onBcfgConfig,
   NIL_EVENT_TAG,             NULL
};
PAGE_DESC base_page = { "MS_BasePage", 0, MS_PageEvents };

void elf_exit ( )
{
   //stop ( pid );

   kill_data ( &ELF_BEGIN, (void(*)(void*)) mfree_adr ( ) );
}

void onCloseMSBook (BOOK *book )
{  Timer_Kill(&timer); Timer_Kill(&timer2); 
   //kill_proc ( pid );
   SUBPROC ( elf_exit );
}

OS_PROCESS(my_proc)
{
  while ( true );
}





int main ( wchar_t *elfname, wchar_t *path, wchar_t *fname)
{
   MyBOOK * MSBook = new MyBOOK;

   memset ( MSBook, 0, sizeof ( MyBOOK ) );
   CreateBook ( MSBook, onCloseMSBook, &base_page, "Memory View", -1, 0 );
   MMSBook = MSBook;
   InitConfig ( );
   ImageID_GetIndirect ( (void *)icon, icon_size, 0, L"png", &imageID ); // ikonka w rogu
  
   
   if (path && fname)
  {
    fliemode=1;
    wstrncpy(MSBook->path,path,MAXELEMS(MSBook->path));
    wstrncpy(MSBook->fname,fname,MAXELEMS(MSBook->fname));
    MSBook->file_open=1;
   // RegisterImage(path,fname);
   // openFile(path, fname,FILE_MODE_READ, 1);
    
    CreateBoardGui(MSBook);
    ShowWindow(MSBook->board);
  }
  else
  {   
    CreateBoardGui(MSBook);
    ShowWindow(MSBook->board);
  }

   //zpid = create_process(0,"memoryv",my_proc,1024,(OSPRIORITY)31 ,0,0,0,0,0);
   //pid = GetCurrentPID();
   //start ( pid );
//memcpy((int*)0x4c00a96e,"eloxxx mUUUU",11);
   //BookObj_GotoPage ( &MSBook->book, &bk_main );
  
   return 0;
}
